//
//  QuestionDatabase.cpp
//  GirlsNightOut
//
//  Created by Jeppe Nielsen on 8/23/14.
//  Copyright (c) 2014 Jeppe Nielsen. All rights reserved.
//

#include "QuestionDatabase.hpp"
#include <iostream>
#include <sstream>
#include "StringHelper.hpp"
#include "minijson.h"

using namespace std;

const QuestionDatabase::Questions& QuestionDatabase::GetQuestions() {
    return questions;
}

static void ShowValue(string id, minijson::value& value) {
    
    switch (value.getType()) {
        case minijson::type::object_type: {
            minijson::object& o = value.get<minijson::object>();
            for (minijson::object::iterator it = o.begin(); it!=o.end(); ++it) {
                ShowValue(id +"/" + it->first, it->second);
            }
            break;
        }
        case minijson::type::string_type: {
            cout<<id<<" -> "<<value.get<string>()<<endl;
            break;
        }
        case minijson::type::number_type: {
            cout<<id<<" -> "<<value.get<minijson::number>()<<endl;
            break;
        }
        case minijson::type::boolean_type: {
            cout<<id<<" -> "<<value.get<minijson::boolean>()<<endl;
            break;
        }
        case minijson::type::array_type: {
            minijson::array array = value.get<minijson::array>();
            for (minijson::array::iterator it = array.begin(); it!=array.end(); ++it) {
                ShowValue("array element", *it);
            }
            break;
        }
            
        default:
            break;
    }
    
}


void QuestionDatabase::UpdateFromJSON(std::string json) {
    
    const char* jsonCString = json.c_str();
    
    minijson::value root;
    
    minijson::error e = minijson::parse(jsonCString, root);
    if (e == minijson::no_error) {
        ParseJson(root);
        //ShowValue("", root);
    } else {
        std::cout<<"Error parsing json:"<<std::endl<<minijson::errstr(e)<<std::endl<<json<<std::endl;
    }
}

void QuestionDatabase::ParseJson(minijson::value& value) {
    
    switch (value.getType()) {
        case minijson::type::object_type: {
            minijson::object& o = value.get<minijson::object>();
            
            int id = -1;
            for (auto it = o.begin(); it!=o.end(); ++it) {
                if (it->first == "id") {
                    id = (int)atoi(it->second.get<minijson::string>().c_str());
                    break;
                }
            }
            if (id>=0) {
                Question* question = 0;
                for (auto it = o.begin(); it!=o.end(); ++it) {
                    if (it->first == "question") {
                        question = &questions[id];
                        question->id = id;
                        std::string base64Encoded =it->second.get<minijson::string>();
                        std::string text = Nano::StringHelper::base64_decode(base64Encoded);
                        question->question = text;
                    } else if (it->first == "answer") {
                        question = &questions[id];
                        question->id = id;
                        std::string base64Encoded =it->second.get<minijson::string>();
                        std::string text = Nano::StringHelper::base64_decode(base64Encoded);
                        question->answer = text;
                    } else if (it->first == "categoryID") {
                        question = &questions[id];
                        question->categoryID = (int)atoi(it->second.get<minijson::string>().c_str());
                    }
                }
            }
            
            break;
        }
        case minijson::type::string_type: {
            //cout<<id<<" -> "<<value.get<string>()<<endl;
            break;
        }
        case minijson::type::number_type: {
            //cout<<id<<" -> "<<value.get<minijson::number>()<<endl;
            break;
        }
        case minijson::type::boolean_type: {
            //cout<<id<<" -> "<<value.get<minijson::boolean>()<<endl;
            break;
        }
        case minijson::type::array_type: {
            minijson::array array = value.get<minijson::array>();
            for (minijson::array::iterator it = array.begin(); it!=array.end(); ++it) {
                ParseJson(*it);
            }
            break;
        }
            
        default:
            break;
    }
    
}

Question* QuestionDatabase::GetQuestion(int index) {
    if (questions.empty()) return 0;
    int i = index % questions.size();
    int counter = 0;
    for (auto it = questions.begin(); it!=questions.end(); ++it) {
        if (counter == i) return &it->second;
        counter++;
    }
    return 0;
}
